/*
 * Connection.java
 *
 * Created on 18 lipiec 2007, 12:18
 *
 */

package pl.krakow.ae.knp.wikiboard.communication;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import pl.krakow.ae.knp.wikiboard.gui.palette.OtherPaletteEvent;
import pl.krakow.ae.knp.wikiboard.gui.palette.OtherPaletteEventType;
import pl.krakow.ae.knp.wikiboard.gui.palette.PaletteEventListener;
import pl.krakow.ae.knp.wikiboard.gui.userslist.UsersList;
import pl.krakow.ae.knp.wikiboard.utils.Address;

/**
 *
 * @author slawek
 */
public class Connection {
    
    private static Socket socket;
    private static ObjectInputStream in;
    private static ObjectOutputStream out;
    
    private static boolean leader;
    
//    private static BoardEventListener listener;
    private static Receiver receiver;
    private boolean connected = false;
    
    private PaletteEventListener listener;
    
    private OtherPaletteEventType opet = OtherPaletteEventType.USERNAME_EXISTS;
    
    /**
     * Creates a new instance of Connection
     */
    public Connection(PaletteEventListener listener){
        this.listener=listener;
    }
    
//    public static void addBoardEventListener(BoardEventListener list) {
//        listener = list;
//    }
    
    public void connect(String address, String userName) throws ClassNotFoundException{
        Address a = new Address(address);
        try {
            socket =  new Socket( a.getHost(), a.getPort() );
            in = new ObjectInputStream( socket.getInputStream() );
            out = new ObjectOutputStream( socket.getOutputStream() );
            // wysylam nazwe uzytkownika
            out.writeObject(userName);
            String userNameResponse = (String)in.readObject();
            System.out.println("Odebralem: " +userNameResponse);
            if (userNameResponse.equals("username occupied")) {
                listener.paletteModified( new OtherPaletteEvent( opet ) );
                return;
            }
            
            receive();
            
            System.out.println("Server - port: " + socket.getPort());
            System.out.println("Server - localPort: " + socket.getLocalPort());
            this.connected = true;
        } catch (ConnectException ex){
            System.out.println("Serwer nie odpowiada");
        } catch (UnknownHostException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public void disconnect() {
        try {
            in.close();
            out.close();
            socket.close();
            this.connected = false;
            System.out.println("Disconnected");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
//    public static void send(BoardEvent event) {
//        if(leader)
//            new Sender(out).sendEvent(event);
//    }
    
    private void receive() {
        
        receiver = new Receiver(in);
    }
    
    public void addListener(UsersList usersList) {
        if( receiver != null)
            receiver.addListener(usersList);
    }
    
    public boolean isConnected() {
        return this.connected;
    }
}
